#include "ShadowApp.h"
#include "NeroGame\Nero.h"
#include "Win32\Win32.h"
#include "DirectX\DirectX11.h"

using namespace Game;
RTTI_DEFINITIONS(ShadowApp)

//ShadowApp::ShadowApp(void)
//	:Game::NGame(),
//	 mCurrentState(DEPTH_STATE)
//{
//}
//
//ShadowApp::~ShadowApp(void)
//{
//}
//
//void ShadowApp::Initialize(const std::string& scene)
//{
//	GetPlatform()->GetCamera()->SetPosition(NVector3(0.0f, 0.0f, -10.0f));
//
//	mFloor =  MY_NEW(Memory::HID_Game, "Floor") Floor();
//	mFloor->Initialize("Plane");
//	mFloor->SetPosition(D3DXVECTOR3(0.0f, 1.0f, 0.0f));
//
//	mCube = MY_NEW(Memory::HID_Game, "Cube") Cube();
//	mCube->Initialize();
//	mCube->SetPosition(D3DXVECTOR3(-2.0f, 2.0f, 0.0f));
//
//	mSphere = MY_NEW(Memory::HID_Game,"Ball") Ball();
//	mSphere->Initialize();
//	mSphere->SetPosition(D3DXVECTOR3(2.0f, 2.0f, 0.0f));
//
//	mDepthShader = MY_NEW(Memory::HID_Rendering, "DepthShader") Graphics::DepthShader();
//	mDepthShader->Initialize();
//
//	mLight = MY_NEW(Memory::HID_Rendering, "Light") Graphics::Light();
//	mLight->Initialize();
//	mLight->SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f);
//	mLight->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
//	mLight->SetLookAt(0.0f, 0.0f, 0.0f);
//	mLight->GenerateProjectionMatrix(SCREEN_DEPTH, SCREEN_NEAR);
//
//	mShadowShader = MY_NEW(Memory::HID_Rendering, "Shadow") Graphics::ShadowShader();
//	mShadowShader->Initialize();
//
//	mRenderTexture = MY_NEW(Memory::HID_Rendering, "Render Target") Graphics::RenderTexture();
//	mRenderTexture->Initialize(1280, 720, 100, 100);
//}
//
//void ShadowApp::Deinitialize()
//{
//	MY_DELETE(mDepthShader);
//	MY_DELETE(mFloor);
//	MY_DELETE(mCube);
//	MY_DELETE(mSphere);
//	MY_DELETE(mDepthShader);
//	MY_DELETE(mLight);
//	MY_DELETE(mShadowShader);
//	MY_DELETE(mRenderTexture);
//}
//
//void ShadowApp::Update(float gameTime)
//{
//	static float lightPositionX = -5.0f;
//
//	lightPositionX += 0.05f;
//	if(lightPositionX > 5.0f)
//	{
//		lightPositionX = -5.0f;
//	}
//
//	mLight->SetPosition(lightPositionX, 8.0f, -5.0f);
//}
//
//void ShadowApp::Render()
//{
//	D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix;
//	Graphics::D3DDevice* device = GetPlatform()->GetDevice()->As<Graphics::D3DDevice>();
//
//	worldMatrix = device->GetWorldMatrix();
//	projectionMatrix = device->GetProjectionMatrix();
//	viewMatrix = Graphics::Utility::NeroToDXD(GetPlatform()->GetCamera()->GetViewMatrix());
//
//	switch (mCurrentState)
//	{
//	case ShadowApp::NULL_STATE:
//		break;
//	case ShadowApp::DEPTH_STATE:
//		GetPlatform()->GetCamera()->SetPosition(NVector3(0.0f,2.0f, -10.0f));
//		mFloor->Render();
//		mDepthShader->Render(mFloor->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix);
//		break;
//	case ShadowApp::SHADOW_STATE_PRE:
//		mLight->GenerateViewMatrix();
//		lightViewMatrix = mLight->GetViewMatrix();
//		lightProjectionMatrix = mLight->GetProjectionMatrix();
//
//		/* Render Cube	*/
//		D3DXMatrixTranslation(&worldMatrix, mCube->GetPosition().x, mCube->GetPosition().y, mCube->GetPosition().z);
//		mCube->Render();
//		mDepthShader->Render(mCube->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);
//		
//		worldMatrix = device->GetWorldMatrix();
//		/* Render Sphere */
//		D3DXMatrixTranslation(&worldMatrix, mSphere->GetPosition().x, mSphere->GetPosition().y, mSphere->GetPosition().z);
//		mSphere->Render();
//		mDepthShader->Render(mSphere->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);
//	
//		worldMatrix = device->GetWorldMatrix();
//		/* Render Floor	*/
//		D3DXMatrixTranslation(&worldMatrix, mFloor->GetPosition().x, mFloor->GetPosition().y, mFloor->GetPosition().z);
//		mFloor->Render();
//		mDepthShader->Render(mFloor->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);
//
//		break;
//	case ShadowApp::SHADOW_STATE:
//		GetPlatform()->GetCamera()->SetPosition(NVector3(0.0f, 3.0f, -10.0f));
//
//		mLight->GenerateViewMatrix();
//		lightViewMatrix = mLight->GetViewMatrix();
//		lightProjectionMatrix = mLight->GetProjectionMatrix();
//		worldMatrix = device->GetWorldMatrix();
//
//		/* Render Cube	*/
//		D3DXMatrixTranslation(&worldMatrix, mCube->GetPosition().x, mCube->GetPosition().y, mCube->GetPosition().z);
//		mCube->Render();
//		mShadowShader->Render(mCube->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, 
//							  lightViewMatrix, lightProjectionMatrix, 
//							  Graphics::TextureResourceManager::GetInstance()->GetResource("Wall"), 
//							  &Graphics::Texture("Render Texture", mRenderTexture->GetShaderResourceView()), 
//							  mLight);
//
//		worldMatrix = device->GetWorldMatrix();
//		/* Render Sphere */
//		D3DXMatrixTranslation(&worldMatrix, mSphere->GetPosition().x, mSphere->GetPosition().y, mSphere->GetPosition().z);
//		mSphere->Render();
//		mShadowShader->Render(mSphere->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, 
//							  lightViewMatrix, lightProjectionMatrix, 
//							  Graphics::TextureResourceManager::GetInstance()->GetResource("Ice"), 
//							  &Graphics::Texture("Render Texture", mRenderTexture->GetShaderResourceView()), 
//							  mLight);
//
//		worldMatrix = device->GetWorldMatrix();
//		/* Render Floor	*/
//		D3DXMatrixTranslation(&worldMatrix, mFloor->GetPosition().x, mFloor->GetPosition().y, mFloor->GetPosition().z);
//		mFloor->Render();
//		mShadowShader->Render(mFloor->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, 
//							  lightViewMatrix, lightProjectionMatrix, 
//							  Graphics::TextureResourceManager::GetInstance()->GetResource("Metal"), 
//							  &Graphics::Texture("Render Texture", mRenderTexture->GetShaderResourceView()), 
//							  mLight);
//
//		break;
//
//	default:
//		break;
//	}
//}
//
//void ShadowApp::PreRender()
//{
//	System::Win32Graphics* graphics = (System::Win32Graphics*)GetPlatform()->GetGraphics();
//	Graphics::Camera* camera = GetPlatform()->GetCamera()->As<Graphics::Camera>();
//
//	if(camera)
//	{
//		mCurrentState = SHADOW_STATE_PRE;
//
//		graphics->RenderToTexture(mRenderTexture, 0.0f);
//			
//		mCurrentState = SHADOW_STATE;
//	}
//}